define([
    'jquery',
    'underscore',
    'backbone',
    'models/message',
    'collections/messages',
    'text!templates/message/message-new.html',
    'iscroll'
], function ($, _, Backbone, MessageModel,MessageCollection,template) {
    var NewMessageForm = Backbone.View.extend({

        tagName:"div", // Not required since 'div' is the default if no el or tagName specified
        template : _.template(template),
        initialize:function () {
            /*this.model.bind("change", this.render, this);*/
        },

        render:function (eventName) {
            $(this.el).html(this.template(this.model.toJSON()));
            return this;
        },

        events:{
            "change input":"change",
            "click .send":"sendMessage"
//            "click .save":"saveMessage"
        },

        change:function (event) {
            var target = event.target;
            console.log('changing ' + target.id + ' from: ' + target.defaultValue + ' to: ' + target.value);
            // You could change your model on the spot, like this:
            // var change = {};
            // change[target.name] = target.value;
            // this.model.set(change);
        },

        sendMessage:function () {
            var messageModel=new MessageModel;
            var MessagesClt = new MessageCollection;

            messageModel.set({
                to:$('#to-people').val(),
                subject:$('#subject').val(),
                message:$('#message').val(),
                saveOnly:0
            });

            //var self = this;
//            if (this.model.isNew()) {
                MessagesClt.create(messageModel, {
                    success:function (data,response) {
                        var thread=eval(response);
                        console.log(thread);
                        var pri_id=thread.thread_id;
                        app.threadMessageList=false;
                       // app.threadMessage();
                        app.threadMessageDetail(pri_id);
                        app.navigate("thread/"+pri_id);
                        //app.messageDetails(pri_id);
                        //app.navigate('thread/message');
                    }
                });
//            } else {
//                this.model.save(this.model, {
//                    success:function () {
//                        app.messagedDetails(self.model.id);
//                        app.navigate('message/' + self.model.id);
//                    }
//                });
//            }

            return false;
        },

        /*saveMessage:function () {
            this.model.set({
                to:$('#to-people').val(),
                subject:$('#subject').val(),
                message:$('#message').val(),
                saveOnly:1
            });

            var self = this;
            if (this.model.isNew()) {
                app.messageList.create(this.model, {
                    success:function () {
                        app.messageDetails(self.model.id);
                        app.navigate('message/' + self.model.id);
                    }
                });
            } else {
                this.model.save(this.model, {
                    success:function () {
                        app.messageDetails(self.model.id);
                        app.navigate('message/' + self.model.id);
                    }
                });
            }

            return false;
        },*/

        deleteMessage:function () {
            this.model.destroy({
                success:function () {
                    alert('Message deleted successfully');
                    app.list();
                    app.navigate('message/new');
                }
            });
            return false;
        }

    });
    return NewMessageForm;
});